package kyuu.acshmily.utils
{
	import flash.utils.ByteArray;
	
	import kyuu.acshmily.video.VideoInfo;

	/**
	 * 通用工具类
	 * 包含常用的系统常量和常用的算法
	 */
	public class CommonUtils
	{
		// 播放器总体宽度
		public static const PLAYER_WIDTH:Number = 950;
		// 播放器总体高度
		public static const PLAYER_HEIGHT:Number = 512;

		// 视频组件默认宽度
		public static const VIDEO_WIDTH:Number = 520;
		// 视频组件默认高度
		public static const VIDEO_HEIGHT:Number = 384;

		public static const LOCKED_COMMENT_COLOR:int = 0x0000ff;

		// 视频缓冲时间
		public static const BUFFER_TIME:Number = 5;
		// 视频缺省音量
		public static const DEFAULT_VOLUMN:Number = 0.7;

		// 加载失败时，空视频的位置和长度
		public static const NULL_FLV_URL:String = "http://localhost/null.flv";
		public static const NULL_FLV_LENGTH:Number = 4.68;

		public function CommonUtils(){}
		
		/**
		 * 将秒数格式化成MM:SS的形式
		 * @param time - 时间值
		 */
		public static function formatSecond(time:Number):String{
			var second:String = (100 + Math.floor(time % 60)).toString().substr(1);
			var minute:String = (100 + Math.floor(time / 60)).toString().substr(1);
			return minute + ":" + second;
		}
		public static function formatMillisecond(ms:Number):String{
			return formatSecond(ms / 1000);
		}

		/**
		 * 获取当前时间字符串
		 */
		public static function getNowString():String{
			var dt:Date = new Date();
			var now:String = dt.fullYear.toString() + '-' + (dt.month+1).toString() + '-' + dt.date.toString();
			now += ' ' + dt.hours.toString() + ':' + dt.minutes.toString() + ':' + dt.seconds.toString();
			return now;
		}

		/**
		 * 去除字符串的首尾空格
		 */
		public static function trim(str:String):String{
			return str.replace(/^\s*/, "").replace(/\s*$/, "");
		}

		/**
		 * 深度克隆一个对象
		 * @param source - 要克隆的对象
		 */
		public static function deepCopy(source:Object):Object{
			var buffer:ByteArray = new ByteArray();
			buffer.writeObject(source);
			buffer.position = 0;
			var result:Object = buffer.readObject();
			return result;
		}

		/**
		 * 克隆视频信息数组
		 * @param videoInfos - 要克隆的视频数组
		 */
		public static function copyVideoInfos(videoInfos:Array):Array{
			if(videoInfos == null) return null;
			var result:Array = new Array();
			for(var i:int=0; i<videoInfos.length; i++){
				var vi:VideoInfo = videoInfos[i] as VideoInfo;
				if(vi == null) continue;

				var vi2:VideoInfo = new VideoInfo(vi.partName, vi.videoUrl, vi.videoLength, vi.videoWidth, vi.videoHeight);
				result.push(vi2);
				vi = null;
			}
			return result;
		}
	}
}